import 'package:flutter/cupertino.dart';

// 对 int 类型扩展
extension IntFix on int {
  double get px {
    return ScreenAdapterHelper.getPx(toDouble());
  }
}
// 扩展 double 类型，用法：200.2.px
// 调用 ScreenAdapterHelper.getPx 转换 200.2 为实际屏幕大小
extension DoubleFix on double {
  double get px {
    return ScreenAdapterHelper.getPx(this);
  }
}

/**
 * 屏幕适配工具类
 */
class ScreenAdapterHelper {
  static late MediaQueryData _mediaQueryData;
  static late double screenWidth;
  static late double screenHeight;
  // static double ratio = 1;
  static late double ratio;

  /**
   * 根据设计稿宽度获取屏幕宽高
   * @Param craftWidth: 设计稿的宽度，默认375
   */
  static init(BuildContext context, {double craftWidth = 375}){
    _mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData.size.width;
    screenHeight = _mediaQueryData.size.height;
    ratio = screenWidth / craftWidth;// 根据屏幕宽度和设计稿宽度计算系数
  }

  // 设计稿大小乘以系数后得到实际屏幕的大小
  static double getPx(double craftPx){
    return ScreenAdapterHelper.ratio * craftPx;
  }
}